<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class modelstatistics extends model
{
	
	function __construct(  )
	{
		
		
		
	}
	
	public function getList( $user_id, $key, $limit=10, $offset=0 ){
				
		$result=false;
		
		try{
				 		
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$pagination = sprintf(" LIMIT %u ",$limit);	
			$pagination = $pagination.sprintf(" OFFSET %u ",$offset);
			
			$query = sprintf("SELECT  a.key_data,
										a.date_access,
										a.from_address,
										a.notation,
										
										CASE WHEN a.metadata=0 THEN 'Não'
								            WHEN a.metadata=1 THEN 'Sim'
								       END metadata
										
										
							FROM \"%s%s\" a
							INNER JOIN \"%s%s\" b ON a.key_data = b.table_name							
							 WHERE b.user_id=%u and a.key_data='%s'							 
							
							ORDER BY a.date_access desc
							%s;",
						configuration::table_prefix,
	                 	configuration::table_access_statistics_download,
	                 	configuration::table_prefix,
	                 	configuration::table_view, 
	                 	$user_id, 
						$key,
						$pagination);
		
		// /exit($query);

			$result = $db->getQuery($query);
			
			//var_dump( $result );exit();

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}


	
	public function getAccessKey( $key, $task_="", $notation="" ){
				
		$result=false;
		
		try{
					
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
			
			switch($task_){
				
				case 'all':
					$metadata="";
					break;
				case 'metadata':
					$metadata=sprintf(" and metadata=%u ", 1);
					break;
				case 'download':
					$metadata=sprintf(" and metadata=%u ", 0);
					break;
				default:
					$metadata="";
			}
				
				
			
			if(!empty($notation))
				$notation=sprintf(" and notation='%s' ", $notation);
			
				
				$query = sprintf("SELECT count(*) as count_access 
							FROM \"%s%s\" WHERE key_data='%s'
							%s %s;",
							configuration::table_prefix,
		                 	configuration::table_access_statistics_download, 
		                 	$key,
							$notation,
							$metadata);	
							
			//exit($query);
	
			$result = $db->getQuery($query);
	
			//$result = $result[0]->count_access;
			
			$result = array((object) array("key"=>$key, 
										"access"=>$result[0]->count_access));

		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}


	public function getAccessKeyAllStatistics( $key ){
				
		$result=false;
		
		try{
			
			$access = $this->getAccessKey($key,"all",""); 
			$accessAll = $access[0]->access;
			
			$access = $this->getAccessKey($key,"metadata","");
			$accessMetadataAll = $access[0]->access;
			
			$access = $this->getAccessKey($key,"download","");
			$accessDownloadAll = $access[0]->access;
			
			$access = $this->getAccessKey($key,"download","csv");
			$accessDownloadNotationCSV = $access[0]->access;
			
			$access = $this->getAccessKey($key,"download","json");
			$accessDownloadNotationJSON = $access[0]->access;
			
			$access = $this->getAccessKey($key,"metadata","csv");
			$accessMetadataNotationCSV = $access[0]->access;
			
			$access = $this->getAccessKey($key,"metadata","json");
			$accessMetadataNotationJSON = $access[0]->access;
					
			$result = array((object) array("key"=>$key, 
			"access"=>$accessAll,
			"accessMetadataAll"=>$accessMetadataAll,
			"accessDownloadAll"=>$accessDownloadAll,
			"accessDownloadNotationCSV"=>$accessDownloadNotationCSV,
			"accessDownloadNotationJSON"=>$accessDownloadNotationJSON,
			"accessMetadataNotationCSV"=>$accessMetadataNotationCSV,
			"accessMetadataNotationJSON"=>$accessMetadataNotationJSON));
								
		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}

	
}